const { createWebAPIRequest } = require('../../util/util')
const request = require('request')

module.exports = [
  {
    /**
    * @api {get} /music/mv [mv详情]
    * @apiGroup Music
    * @apiPermission none
    * @apiParam {Number} mvid
    */
    url: '/music/mv',
    request: (req, res) => {
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const mvid = req.query.mvid
      const data = {
        id: mvid
      }

      createWebAPIRequest(
        'music.163.com',
        `/weapi/mv/detail`,
        'POST',
        data,
        cookie,
        musicReq => {
          res.send(musicReq)
        },
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {get} /music/mv/first [最新 mv]
    * @apiGroup Music
    * @apiPermission none
    * @apiParam {Number} [offset=0] 页
    * @apiParam {Number} [limit=20]  每页数量
    */
    url: '/music/mv/first',
    request: (req, res) => {
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const data = {
        offset: req.query.offset || 0,
        total: true,
        limit: req.query.limit || 20,
        csrf_token: ''
      }
      createWebAPIRequest(
        'music.163.com',
        '/weapi/mv/first',
        'POST',
        data,
        cookie,
        musicReq => res.send(musicReq),
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {get} /music/mv/url [播放mv Url]
    * @apiGroup Music
    * @apiPermission none
    * @apiParam {String} url
    */
    url: '/music/mv/url',
    request: (req, res) => {
      const url = req.query.url
      const headers = {
        Referer: 'http://music.163.com/',
        Cookie: 'appver=1.5.0.75771;',
        'Content-Type': 'video/mp4',
        Location: url
      }
      const options = {
        header: headers,
        url: url
      }
      request(options)
        .on('error', err => {
          res.send({ err })
        })
        .pipe(res)
    }
  }
]
